//
//  YDChooseCityBankView.m
//  PeiZiWangBei
//
//  Created by 巨彦新 on 2017/12/7.
//  Copyright © 2017年 LK-IOS. All rights reserved.
//

#import "YDChooseCityBankView.h"

@interface YDChooseCityBankView()<UITextFieldDelegate>

@property (nonatomic, strong)UITextField *bankTextField; //支行输入框

@property (nonatomic, strong)NSMutableArray *cityTextFieldArr;

@property (nonatomic, strong)UITextField *cityTextField;

@property (nonatomic, strong)UILabel *line;


@end

@implementation YDChooseCityBankView


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor colorWithHexString:mainGray];
//        [self addCityTextField];
//        [self bankTextField];
    }
    return self;
}

-(void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    [self addCityTextField];
    [self bankTextField];
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    self.chooseType = textField.tag - 100;
    if (self.chooseTextBack) {
        self.chooseTextBack(textField.tag - 100);
    }
    [textField resignFirstResponder];
    return NO;
    
}


-(void)setText:(NSString *)text
{
    
    switch (self.chooseType) {
        case Choose_Province:
            ((UITextField *)[self.cityTextFieldArr firstObject]).text = text;
            break;
        case Choose_City:
            ((UITextField *)[self.cityTextFieldArr lastObject]).text = text;
            break;
        case Choose_Bank:
            self.bankTextField.text = text;
            break;
        default:
            break;
    }
}

-(NSString *)text
{

    switch (self.chooseType) {
        case Choose_Province:
          return ((UITextField *)[self.cityTextFieldArr firstObject]).text;

        case Choose_City:
          return  ((UITextField *)[self.cityTextFieldArr lastObject]).text;
            break;
        case Choose_Bank:
          return   self.bankTextField.text;
            break;
        default:
            return nil;
            break;
    }
}
-(void)setIsHideCity:(BOOL)isHideCity
{
    _isHideCity = isHideCity;
    self.cityTextField.hidden  = isHideCity;
    self.line.hidden = isHideCity;
    
}
- (void)addCityTextField
{
    
    for (int i  = 0; i < 2; i++) {
        UITextField *cityTextField = [[UITextField alloc] init];
        [self addSubview:cityTextField];
        cityTextField.tag = 100 + i;
        
        cityTextField.placeholder = !i ?  @"请选择省份" :@"请选择城市";
        cityTextField.borderStyle = UITextBorderStyleNone;
        cityTextField.adjustsFontSizeToFitWidth = YES;
        cityTextField.font = [UIFont systemFontOfSize:font14ScreenWRate *LKScreenW];
        cityTextField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 8, 0)];
        cityTextField.delegate = self;
        //设置显示模式为永远显示(默认不显示 必须设置 否则没有效果)
        cityTextField.leftViewMode = UITextFieldViewModeAlways;
        [self.cityTextFieldArr addObject:cityTextField];
        if (i == 1) {
            self.cityTextField = cityTextField;
        }
        
    }
    
    
    //水平方向控件间隔固定等间隔
    [self.cityTextFieldArr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal
                                 withFixedSpacing:0
                                      leadSpacing:rate375(10)
                                      tailSpacing:rate375(10)];
    
    [self.cityTextFieldArr mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(isPhoneX ? @(rateX812(14)) : @(rate750(14)));
        make.top.equalTo(isPhoneX ? @(rateX812(16)) : @(rate750(16)));
    }];
   
    UILabel *gapLbl = [UILabel new];
    gapLbl.backgroundColor = NumberColor(216, 216, 216);
    [self addSubview:gapLbl];
    [gapLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.width.equalTo(@1);
        make.height.equalTo(isPhoneX ? @(rateX812(14)) : @(rate750(14)));
        make.top.equalTo(isPhoneX ? @(rateX812(16)) : @(rate750(16)));
    }];
    
    self.line = gapLbl;
    CGContextRef context=UIGraphicsGetCurrentContext();//获取绘图用的图形上下文

    CGFloat lengths[] = {4};
    CGContextSetLineDash(context, 4, lengths,1);

    //上面是准备工作，下面开始画线了
    
     CGFloat lineY = isPhoneX ? rateX812(49) : rate750(49);
    CGContextSetStrokeColorWithColor(context, [UIColor lightGrayColor].CGColor);//设置线的颜色
    CGContextMoveToPoint(context,rate375(10),lineY);//画线的起始点位置
    CGContextAddLineToPoint(context,self.frame.size.width - 2 *rate375(10) ,lineY);//画第一条线的终点位置

    CGContextStrokePath(context);//把线在界面上绘制出来
 
  

}



- (UITextField *)bankTextField
{
    if (!_bankTextField) {
        _bankTextField = [UITextField new];
        [self addSubview:_bankTextField];
    
        
        _bankTextField.placeholder = @"请选择支行";
        _bankTextField.borderStyle = UITextBorderStyleNone;
        _bankTextField.font = [UIFont systemFontOfSize:font14ScreenWRate *LKScreenW];
        _bankTextField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 8, 0)];
        //设置显示模式为永远显示(默认不显示 必须设置 否则没有效果)
        _bankTextField.leftViewMode = UITextFieldViewModeAlways;
        _bankTextField.delegate = self;
        _bankTextField.tag = 102;
        _bankTextField.adjustsFontSizeToFitWidth = YES;
        [_bankTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(rate375(10)));
            make.right.equalTo(@(rate375(-10)));
            make.top.equalTo(isPhoneX ? @(rateX812(65)) : @(rate750(65)));
        }];
    }
    return _bankTextField;
}
-(NSMutableArray *)cityTextFieldArr
{
    if (!_cityTextFieldArr) {
        _cityTextFieldArr = [[NSMutableArray alloc] initWithCapacity:2];
    }
    return _cityTextFieldArr;
}
@end
